package com.pan.service;

import com.pan.dao.User;
import com.pan.dao.UserDAO;
import com.pan.dao.UserDAOImpl;
import com.pan.util.CheckAuth;
import com.pan.util.ViewBaseServlet;
import lombok.Getter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.pan.constants.DatabaseInfo;

@WebServlet("/filelist")
public class FileBrowserController extends ViewBaseServlet {

    @Getter
    private class MyFile {
        public String name; // 文件名
        public String type; // 是否为文件夹
        public long size;   // 文件大小
        public Date uploadTime; // 上传时间

        public MyFile(String name, String type, long size, Date uploadTime) {
            this.name = name;
            this.type = type;
            this.size = size;
            this.uploadTime = uploadTime;
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = request.getParameter("token");
        if (!CheckAuth.checkToken(token)) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
            return;
        }
        UserDAO userDAO = UserDAOImpl.getInstance();
        User user = userDAO.getUserByToken(token);

        String filePath = DatabaseInfo.FILE_PATH + "\\" + user.getFilePath() + "\\";
        String path = request.getParameter("path");
        // 进行 URL 解码
        if (path != null) {
            path = java.net.URLDecoder.decode(path, "UTF-8");
        }
        List<String> pathList = new ArrayList<String>();
        pathList.add("我的文件");

        boolean isRoot = true;

        if (path != null && !path.isEmpty()) {
            isRoot = false;
            filePath += path;
            String[] paths = path.split("\\\\");
            for (String p : paths) {
                pathList.add(p);
            }
        }

        List<MyFile> fileList = getAllFiles(filePath);

        request.setAttribute("username", user.getUserName());
        request.setAttribute("fileList", fileList);
        request.setAttribute("pathList", pathList);
        request.setAttribute("isRoot", isRoot);
        this.processTemplate("file", request, response);
    }

    private List<MyFile> getAllFiles(String filePath) {
        File Dir = new File(filePath);
        File[] files = Dir.listFiles();
        List<MyFile> fileList = new ArrayList<MyFile>();
        if (files != null) {
            for (File file : files) {
                String name = file.getName();
                String type = file.isDirectory() ? "folder" : "file";
                long size = file.length();
                Date uploadTime = new Date(file.lastModified());
                MyFile myFile = new MyFile(name, type, size, uploadTime);
                fileList.add(myFile);
            }
        }
        return fileList;
    }
}
